package com.mv.service;

import com.mv.dao.*;
import com.mv.entity.*;
import com.mv.vo.MovieVo;
import com.mv.vo.ReviewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class MovieServiceImpl {

    @Autowired
    MovieMapper movieMapper;

    @Autowired
    CountryMapper countryMapper;

    @Autowired
    TypeMapper typeMapper;

    @Autowired
    ActorMapper actorMapper;

    @Autowired
    ReviewMapper reviewMapper;

    @Autowired
    ConsumerMapper consumerMapper;


    /**
     * 查询指定电影的详细信息，为电影详情页提供信息
     *
     * @param movieId
     * @return
     */
    public MovieVo selectMovieVoDetailById(Integer movieId) {

        MovieVo movieVo = new MovieVo();

        //-- 获取该id对应的电影
        Movie movie = movieMapper.selectByPrimaryKey(movieId);

        //-- 添加电影属性
        movieVo.setMovie(movie);

        //-- 获取该电影对应的地区
        Country country = countryMapper.selectByPrimaryKey(movie.getCountryid());

        //-- 添加地区属性
        movieVo.setCountry(country.getCountry());

        //-- 获取类型集合
        List<Type> typeList = typeMapper.selectTypeForMovieVo(movieId);

        //-- 添加类型属性
        movieVo.setTypeList(typeList);

        //-- 获取演员集合
        List<Actor> actorList = actorMapper.selectActorForMovieVo(movieId);

        //-- 添加演员属性
        movieVo.setActorList(actorList);

        //-- 获取该电影的评论集合
        List<ReviewVo> reviewVoList = new ArrayList<>();

        Review review = new Review();
        review.setMovieid(movieId);
        List<Review> reviewList = reviewMapper.select(review);
        for (Review review1 : reviewList) {
            ReviewVo reviewVo = new ReviewVo();
            reviewVo.setReview(review1);

            Consumer consumer = consumerMapper.selectByPrimaryKey(review1.getConsumerid());
            reviewVo.setConsumer(consumer);

            reviewVoList.add(reviewVo);
        }

        //-- 添加评论属性
        movieVo.setReviewVoList(reviewVoList);

        return movieVo;
    }



    /**
     * 根据类型编号，地区编号，年限，排序信息返回符合条件的电影集合
     *
     * @param typeId
     * @param countryId
     * @param year
     * @return
     * @author 程龙
     */
    public List<Movie> selectMovieByTypeAndTimeAndArea(Integer typeId, Integer countryId, Integer year, Integer orderNum) {

        //-- 最终提交的电影集合
        List<Movie> movieList = new ArrayList<>();

        //-- 地区和类型条件检索
        List<Movie> list = movieMapper.selectMovieByCountryAndType(countryId, typeId);

        //-- 时间条件检索
        for (Movie movie : list) {

            if (year == null) {
                movieList.add(movie);
            } else if (year == 80) {
                if (movie.getDecade() <= 1989 && movie.getDecade() >= 1980) {
                    movieList.add(movie);
                }
            } else if (year == 90) {
                if (movie.getDecade() <= 1999 && movie.getDecade() >= 1990) {
                    movieList.add(movie);
                }
            } else if (year == 10) {
                if (movie.getDecade() <= 2010 && movie.getDecade() >= 2000) {
                    movieList.add(movie);
                }
            } else if (year == 0) {
                if (movie.getDecade() < 1980) {
                    movieList.add(movie);
                }
            } else if (movie.getDecade().equals(year)) {
                movieList.add(movie);
            }
        }

        //--(热度，时间)排序选择
        if (orderNum == 1) {
            //-- 按热度排序
            movieList.sort(new Comparator<Movie>() {
                @Override
                public int compare(Movie o1, Movie o2) {
                    return o2.getHotspot() - o1.getHotspot();
                }
            });
        } else if (orderNum == 2) {
            //-- 按时间排序
            movieList.sort(new Comparator<Movie>() {
                @Override
                public int compare(Movie o1, Movie o2) {
                    return o2.getDecade() - o1.getDecade();
                }
            });
        }

        return movieList;
    }


}
